package android.fwirk;

import java.util.LinkedList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import twitter4j.Status;
import twitter4j.TwitterException;

import com.google.jplurk.Qualifier;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.fwirk.facebook.FacebookStatus;
import android.fwirk.facebook.FacebookUser;
import android.fwirk.obj.CustomProgressDialog;
import android.fwirk.obj.StatusMessage;
import android.fwirk.obj.StatusMessageAdapter;
import android.fwirk.plurk.Plurk;
import android.fwirk.plurk.PlurkUser;
import android.fwirk.twitter.TweetStatus;
import android.fwirk.twitter.TwitterUser;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemLongClickListener;

public class CustomDialogActivity extends Activity implements OnItemLongClickListener, android.content.DialogInterface.OnClickListener, OnClickListener {
	ImageView msgTypeImg;
	TextView msgType;
	WebView msgText;
	LinearLayout plurkList;
	LinearLayout listBox;
	LinearLayout responseBox;
	ListView responseList;
	StatusMessageAdapter msgAdapter;
	EditText response;
	Button retrieve;
	Button reply;
	Button delete;
	Button back;
	AlertDialog resDetails;
	Builder bd;
	StatusMessage empty;
	CustomProgressDialog progress;
	int tID=0;
	boolean gotFacebookStatusResponse;
	boolean gotTweetResponse;
	boolean gotPlurkResponse;
	boolean deletedFacebookStatus;
	boolean deletedTweet;
	boolean deletedPlurk;
	boolean respondFacebookStatus;
	boolean respondTweet;
	boolean respondPlurk;
	int currentType;
	String messageID;
	final int DIALOG_PROGRESS=0;
	String screenName;
	final String HTML_START="<html><body>";
	final String HTML_END="</html></body>";
	final String LINEBREAK="<br/>";
	
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
    	setContentView(R.layout.message);
		msgType = (TextView) findViewById(R.id.msgType);
		msgTypeImg = (ImageView) findViewById(R.id.msgTypeImg);
		msgText = (WebView) findViewById(R.id.msgText);
		plurkList = (LinearLayout) findViewById(R.id.plurkResponse);
		listBox = (LinearLayout) findViewById(R.id.listBox);
		responseBox = (LinearLayout) findViewById(R.id.responseBox);
		responseList=(ListView) findViewById(R.id.msgResponseList);
		response = (EditText) findViewById(R.id.msgResponse);
		retrieve=(Button) findViewById(R.id.retrieveb);
		reply=(Button) findViewById(R.id.replyb);
		delete=(Button) findViewById(R.id.deleteb);
		back=(Button) findViewById(R.id.backb);
		retrieve.setOnClickListener(this);
		reply.setOnClickListener(this);
		delete.setOnClickListener(this);
		back.setOnClickListener(this);
		msgText.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				return true;
			}
		});
		msgAdapter=new StatusMessageAdapter(this, R.layout.row);
    	responseList.setAdapter(msgAdapter);
    	responseList.setOnItemLongClickListener(this);
		bd=new Builder(this);
		bd.setPositiveButton("Back", this);
		empty=new StatusMessage();
    	empty.setDrawID(R.drawable.alert);
    	empty.setStatus("No responses yet.");
    	msgAdapter.add(empty);
    	progress=new CustomProgressDialog(this);
    	progress.setText("Retrieving recent responses...");
        progress.setImage1(R.drawable.facebookd);
		progress.setImage2(R.drawable.twitterd);
		progress.setImage3(R.drawable.plurkd);
		progress.setImage1Visibility(View.GONE);
		progress.setImage2Visibility(View.GONE);
		progress.setImage3Visibility(View.GONE);
		Object status=Const.curStatusMessage.getMsg();
		messageID="0";
		currentType=0;
		gotFacebookStatusResponse=false;
		gotTweetResponse=false;
		gotPlurkResponse=false;
		retrieve.setText("Retrieve");
		String content="";
		if(status instanceof FacebookStatus) {
			currentType=Const.TYPE_FACEBOOK;
		}
		else if(status instanceof TweetStatus) {
			currentType=Const.TYPE_TWITTER;
		}
		else if(status instanceof Plurk) {
			currentType=Const.TYPE_PLURK;
		}
		setMsgType(currentType);
		if(status instanceof FacebookStatus) {
			progress.setImage1Visibility(View.VISIBLE);
			FacebookStatus fs=(FacebookStatus)status;
			FacebookUser user=fs.getOwner();
			content="Poster:"+LINEBREAK+"Anon";
			messageID=fs.getPostID();
			if(user!=null) {
				content="Poster:"+LINEBREAK+user.getName();
			}
			content=content+LINEBREAK+LINEBREAK+"Message:"+LINEBREAK;
			if(fs.getImages()!=null && !fs.getImages().equals("")) {
				Log.d("Test img",fs.getImages());
				content=content+fs.getImages();
			}
			content=content+LINEBREAK+fs.getMessage()+fs.getAttachDesc()+LINEBREAK;
			
			content=content+LINEBREAK+"Time:"+LINEBREAK+DateFormat.format("hh:mm:ss aa - MMM dd, yyyy", fs.getUpdatedTime());
			msgText.loadData(HTML_START+content+HTML_END,"text/html","utf-8");
			if(fs.getStatusID().equals("")) {
				reply.setVisibility(View.VISIBLE);
				retrieve.setVisibility(View.VISIBLE);
				plurkList.setVisibility(View.VISIBLE);
				listBox.setVisibility(View.VISIBLE);
				responseBox.setVisibility(View.VISIBLE);
				if(fs.getAttribution().equals("Fwirk") && fs.getActorID().equals(user.getId())) {
					delete.setVisibility(View.VISIBLE);
				}
				tID++;
				showDialog(DIALOG_PROGRESS);
				new GetResponseThread(this,recentResponseH,messageID,currentType,true,tID%Integer.MAX_VALUE).start();
			}
		}
		else if(status instanceof TweetStatus) {
			progress.setImage2Visibility(View.VISIBLE);
			TweetStatus ts=((TweetStatus)status);
			Status s=ts.getStatus();
			TwitterUser user=ts.getOwner();
			messageID=s.getId()+"";
			screenName="";
			content="Tweet Owner:"+LINEBREAK+"Anon";
			if(user!=null) {
				screenName=user.getScreenName();
				content="Tweet Owner:"+LINEBREAK+s.getUser().getName();
			}
			content=content+LINEBREAK+LINEBREAK+"Message:"+LINEBREAK+s.getText()+LINEBREAK+LINEBREAK+"Time:"+LINEBREAK+DateFormat.format("hh:mm:ss aa - MMM dd, yyyy", s.getCreatedAt());
			if(s.getGeoLocation()!=null) {
				content=content+LINEBREAK+LINEBREAK+"Location:"+LINEBREAK+s.getGeoLocation().getLatitude()+" Lat,"+s.getGeoLocation().getLongitude()+" Long";
			}
			msgText.loadData(HTML_START+content+HTML_END,"text/html","utf-8");
			if(s.getUser().getId()==Const.twitter.getId()) {
				delete.setVisibility(View.VISIBLE);
			}
			else {
				retrieve.setText("Retweet");
				reply.setVisibility(View.VISIBLE);
				retrieve.setVisibility(View.VISIBLE);
				plurkList.setVisibility(View.VISIBLE);
				responseBox.setVisibility(View.VISIBLE);
			}
		}
		else if(status instanceof Plurk) {
			progress.setImage3Visibility(View.VISIBLE);
			Plurk p=(Plurk)status;
			PlurkUser user=p.getOwner();
			messageID=p.getPlurk_id()+"";
			content="Plurker:"+LINEBREAK+"Anon";
			if(user!=null) {
				if(user.getDisplayName().length()>0) {
					content="Plurker:"+LINEBREAK+user.getDisplayName();
				}
				else {
					content="Plurker:"+LINEBREAK+user.getNickName();
				}
			}
			content=content+LINEBREAK+LINEBREAK+"Status:"+LINEBREAK+p.getContentRaw()+LINEBREAK+LINEBREAK+"Time:"+LINEBREAK+DateFormat.format("hh:mm:ss aa - MMM dd, yyyy", p.getPosted())+LINEBREAK+LINEBREAK+"Plurk Type:"+LINEBREAK;
			if(p.getPlurkType()==Plurk.PLURK_TYPE_PRIVATE || p.getPlurkType()==Plurk.PLURK_TYPE_PRIVATE_RESPONDED) {
				content=content+"Private";
			}
			else if(p.getPlurkType()==Plurk.PLURK_TYPE_PUBLIC || p.getPlurkType()==Plurk.PLURK_TYPE_PUBLIC_RESPONDED) {
				content=content+"Public";
			}
			msgText.loadData(HTML_START+content+HTML_END,"text/html","utf-8");
			if(p.getNoComments()!=Plurk.PLURK_NO_COMMENTS) {
				reply.setVisibility(View.VISIBLE);
				retrieve.setVisibility(View.VISIBLE);
				plurkList.setVisibility(View.VISIBLE);
				listBox.setVisibility(View.VISIBLE);
				responseBox.setVisibility(View.VISIBLE);
			}
			if(p.getOwnerID()==Const.plurk.getUserID()) {
				delete.setVisibility(View.VISIBLE);
			}
			tID++;
			showDialog(DIALOG_PROGRESS);
			new GetResponseThread(this,recentResponseH,messageID,currentType,true,tID%Integer.MAX_VALUE).start();
		}
	}
	
	@Override
    protected Dialog onCreateDialog(int id) {
    	if(id==DIALOG_PROGRESS) {
    		return progress.getDialog();
    	}
    	return super.onCreateDialog(id);
    }
	
	public void setMsgType(int type) {
		plurkList.setVisibility(View.GONE);
		listBox.setVisibility(View.GONE);
		responseBox.setVisibility(View.GONE);
		retrieve.setVisibility(View.GONE);
		reply.setVisibility(View.GONE);
		delete.setVisibility(View.GONE);
		if(type==Const.TYPE_FACEBOOK) {
			msgType.setText("Facebook");
			msgTypeImg.setImageResource(R.drawable.facebooke);
		}
		else if(type==Const.TYPE_TWITTER) {
			msgType.setText("Tweet");
			msgTypeImg.setImageResource(R.drawable.twittere);
		}
		else if(type==Const.TYPE_PLURK) {
			msgType.setText("Plurk");
			msgTypeImg.setImageResource(R.drawable.plurke);
		}
		
	}

	public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		if(arg0==responseList) {
			StatusMessage sm=msgAdapter.getItem(arg2);
			if(!(sm.getDrawID()==R.drawable.alert)) {
				Object msg=sm.getMsg();
				String content="";
				if(msg instanceof FacebookStatus) {
					FacebookStatus s=(FacebookStatus) msg;
					FacebookUser user=s.getOwner();
					content="Poster:"+LINEBREAK+"Anon";
					if(user!=null) {
						if(user.getName().length()>0) {
	        				content="Poster:"+LINEBREAK+user.getName();
	        			}
					}
					content=content+LINEBREAK+LINEBREAK+"Message:"+LINEBREAK+s.getMessage().replace("<br/>", "\n")+LINEBREAK+LINEBREAK+"Time:"+LINEBREAK+DateFormat.format("hh:mm:ss aa - MMM dd, yyyy", s.getUpdatedTime());
				}
				else if(msg instanceof Plurk) {
					Plurk p=(Plurk) msg;
					PlurkUser user=p.getOwner();
					content="Poster:"+LINEBREAK+"Anon";
					if(user!=null) {
						if(user.getDisplayName().length()>0) {
	        				content="Plurker:"+LINEBREAK+user.getDisplayName();
	        			}
	        			else {
	        				content="Plurker:"+LINEBREAK+user.getNickName();
	        			}
					}
					content=content+LINEBREAK+LINEBREAK+"Status:"+LINEBREAK+p.getContentRaw()+LINEBREAK+LINEBREAK+"Time:"+LINEBREAK+DateFormat.format("hh:mm:ss aa - MMM dd, yyyy", p.getPosted());
				}
				bd.setMessage(content);
				resDetails=bd.show();
			}
			
		}
		return false;
	}

	public void onClick(DialogInterface arg0, int arg1) {
		if(arg0==resDetails) {
			switch(arg1) {
				case DialogInterface.BUTTON_POSITIVE:
					resDetails.dismiss();
					break;
			}
		}
		
	}

	public void onClick(View v) {
		if(v==retrieve) {
			tID++;
			gotFacebookStatusResponse=false;
			gotTweetResponse=false;
			gotPlurkResponse=false;
			progress.setText("Retrieving recent responses...");
	        progress.setImage1(R.drawable.facebookd);
			progress.setImage2(R.drawable.twitterd);
			progress.setImage3(R.drawable.plurkd);
			progress.setImage1Visibility(View.GONE);
			progress.setImage2Visibility(View.GONE);
			progress.setImage3Visibility(View.GONE);
			if(currentType==Const.TYPE_FACEBOOK) {
				progress.setImage1Visibility(View.VISIBLE);
				showDialog(DIALOG_PROGRESS);
				new GetResponseThread(this,recentResponseH,messageID,currentType,true,tID%Integer.MAX_VALUE).start();
			}
			else if(currentType==Const.TYPE_TWITTER) {
				progress.setImage2Visibility(View.VISIBLE);
				showDialog(DIALOG_PROGRESS);
				new GetResponseThread(this,recentResponseH,messageID,currentType,true,tID%Integer.MAX_VALUE).start();
			}
			else if(currentType==Const.TYPE_PLURK) {
				progress.setImage3Visibility(View.VISIBLE);
				showDialog(DIALOG_PROGRESS);
				new GetResponseThread(this,recentResponseH,messageID,currentType,true,tID%Integer.MAX_VALUE).start();
			}
		}
		else if(v==reply) {
			String message=response.getText().toString();
			if(message.length()==0) {
				Toast.makeText(this, "Please enter something as your reponse", Toast.LENGTH_SHORT).show();
			}
			else {
				respondFacebookStatus=false;
				respondTweet=false;
				respondPlurk=false;
				tID++;
				gotFacebookStatusResponse=false;
				gotTweetResponse=false;
				gotPlurkResponse=false;
				progress.setText("Adding response...");
		        progress.setImage1(R.drawable.facebookd);
				progress.setImage2(R.drawable.twitterd);
				progress.setImage3(R.drawable.plurkd);
				progress.setImage1Visibility(View.GONE);
				progress.setImage2Visibility(View.GONE);
				progress.setImage3Visibility(View.GONE);
				if(currentType==Const.TYPE_FACEBOOK) {
					progress.setImage1Visibility(View.VISIBLE);
					showDialog(DIALOG_PROGRESS);
					new RespondThread(respondH,messageID,message,currentType,tID%Integer.MAX_VALUE).start();
				}
				else if(currentType==Const.TYPE_TWITTER) {
					progress.setImage2Visibility(View.VISIBLE);
					showDialog(DIALOG_PROGRESS);
					new RespondThread(respondH,screenName,message,currentType,tID%Integer.MAX_VALUE).start();
				}
				else if(currentType==Const.TYPE_PLURK) {
					progress.setImage3Visibility(View.VISIBLE);
					showDialog(DIALOG_PROGRESS);
					new RespondThread(respondH,messageID,message,currentType,tID%Integer.MAX_VALUE).start();
				}
			}
		}
		else if(v==delete) {
			deletedFacebookStatus=false;
			deletedTweet=false;
			deletedPlurk=false;
			tID++; 
			progress.setImage1(R.drawable.facebookd);
			progress.setImage2(R.drawable.twitterd);
			progress.setImage3(R.drawable.plurkd);
			progress.setImage1Visibility(View.GONE);
			progress.setImage2Visibility(View.GONE);
			progress.setImage3Visibility(View.GONE);
			progress.setText("Deleting status..."); 
			if(currentType==Const.TYPE_FACEBOOK) {
				progress.setImage1Visibility(View.VISIBLE);
			}
			else if(currentType==Const.TYPE_TWITTER) {
				progress.setImage2Visibility(View.VISIBLE);
			}
			else if(currentType==Const.TYPE_PLURK) {
				progress.setImage3Visibility(View.VISIBLE);
			}
			showDialog(DIALOG_PROGRESS); 
			new DeleteStatusThread(deleteH,messageID,currentType,tID%Integer.MAX_VALUE).start(); 
		}
		else if(v==back) {
			setResult(RESULT_CANCELED);
			finish();
		}
		
	}
	
	final Handler recentResponseH = new Handler() {
        @Override
		public void handleMessage(Message msg) {
        	if(msg.getData().getInt("tID")==tID%Integer.MAX_VALUE) {	        	
	        	if(msg.getData().getBoolean("status") && msg.getData().getInt("type")==Const.TYPE_FACEBOOK) {
	        		gotFacebookStatusResponse=true;
	        		progress.setImage1(R.drawable.facebooke);
	        	}
	        	else if(msg.getData().getBoolean("status") && msg.getData().getInt("type")==Const.TYPE_TWITTER) {
	        		gotTweetResponse = true;
	        		progress.setImage2(R.drawable.twittere);
	        	}
	        	else if(msg.getData().getBoolean("status") && msg.getData().getInt("type")==Const.TYPE_PLURK) {
	        		gotPlurkResponse=true;
	        		progress.setImage3(R.drawable.plurke);
	        	}
	        	else {
	        		Toast.makeText(getApplicationContext(), "Unable to get responses", Toast.LENGTH_SHORT).show();
	        	}
	        	if(gotFacebookStatusResponse || gotTweetResponse || gotPlurkResponse) {
	        		if(gotFacebookStatusResponse) {
	        			msgAdapter.clear();
	        			if(Const.myFacebookComments!=null) {
							for(FacebookStatus s:Const.myFacebookComments) {
								StatusMessage sm=new StatusMessage();
								FacebookUser user=s.getOwner();
								sm.setMsg(s);
								sm.setDrawID(R.drawable.defaultfacebookpic);
								String note="Anon: ";
								if(user!=null) {
									sm.setBitmap(user.getPic());
									if(user.getName().length()>0) {
				        				note=user.getName()+": ";
				        			}
								}
								sm.setStatus(note+s.getMessage());
			        			sm.setMisc(""+DateFormat.format("hh:mm:ss aa - MMM dd, yyyy", s.getUpdatedTime()));
								msgAdapter.add(sm);
							}
		        		}
		        		if(msgAdapter.isEmpty()) {
		        			msgAdapter.add(empty);
		        		}
						Toast.makeText(getApplicationContext(), "Retrieved comments", Toast.LENGTH_SHORT).show();
	        		}
	        		if(gotTweetResponse) {
	        			Toast.makeText(getApplicationContext(), "Retweeted status", Toast.LENGTH_SHORT).show();
	        		}
	        		if(gotPlurkResponse) {
		        		msgAdapter.clear();
		        		if(Const.myPlurkResponses!=null) {
							for(Plurk p:Const.myPlurkResponses) {
								StatusMessage sm=new StatusMessage();
								PlurkUser user=p.getOwner();
								String note="Anon: ";
								sm.setDrawID(R.drawable.defaultplurkpic);
								sm.setMsg(p);		
								if(user!=null) {
									if(user.getPicture()!=null) {
			        					sm.setBitmap(user.getPicture());
			        					if(user.getDisplayName().length()>0) {
					        				note=user.getDisplayName()+": ";
					        			}
					        			else {
					        				note=user.getNickName()+": ";
					        			}
			        				}
				        		}
								sm.setStatus(note+p.getContentRaw());
			        			sm.setMisc(""+DateFormat.format("hh:mm:ss aa - MMM dd, yyyy", p.getPosted()));
								msgAdapter.add(sm);
							}
		        		}
		        		if(msgAdapter.isEmpty()) {
		        			msgAdapter.add(empty);
		        		}
						Toast.makeText(getApplicationContext(), "Retrieved responses", Toast.LENGTH_SHORT).show();
	        		}
	        	}
	        	progress.hide();	
        	}
        }
    };
	
	
	final Handler deleteH = new Handler() {
        @Override
		public void handleMessage(Message msg) {
        	if(msg.getData().getInt("tID")==tID%Integer.MAX_VALUE) {	        	
	        	if(msg.getData().getBoolean("status") && msg.getData().getInt("type")==Const.TYPE_FACEBOOK) {
	        		deletedFacebookStatus=true;
	        		progress.setImage1(R.drawable.facebooke);
	        		Toast.makeText(getApplicationContext(), "Facebook status deleted", Toast.LENGTH_SHORT).show();
	        	}
	        	else if(msg.getData().getBoolean("status") && msg.getData().getInt("type")==Const.TYPE_TWITTER) {
	        		deletedTweet = true;
	        		progress.setImage2(R.drawable.twittere);
	        		Toast.makeText(getApplicationContext(), "Tweet deleted", Toast.LENGTH_SHORT).show();
	        	}
	        	else if(msg.getData().getBoolean("status") && msg.getData().getInt("type")==Const.TYPE_PLURK) {
	        		deletedPlurk=true;
	        		progress.setImage3(R.drawable.plurke);
	        		Toast.makeText(getApplicationContext(), "Plurk deleted", Toast.LENGTH_SHORT).show();
	        	}
	        	else {
	        		Toast.makeText(getApplicationContext(), "Could not delete status", Toast.LENGTH_SHORT).show();
	        	}
	        	progress.hide();
	        	if(deletedFacebookStatus || deletedPlurk || deletedTweet) {
		        	setResult(RESULT_OK);
		        	finish();
	        	}
        	}
        }
    };
    
    final Handler respondH = new Handler() {
        @Override
		public void handleMessage(Message msg) {
        	if(msg.getData().getInt("tID")==tID%Integer.MAX_VALUE) {	        	
	        	if(msg.getData().getBoolean("status") && msg.getData().getInt("type")==Const.TYPE_FACEBOOK) {
	        		respondFacebookStatus=true;
	        		progress.setImage1(R.drawable.facebooke);
	        		Toast.makeText(getApplicationContext(), "Comment added", Toast.LENGTH_SHORT).show();
	        	}
	        	else if(msg.getData().getBoolean("status") && msg.getData().getInt("type")==Const.TYPE_TWITTER) {
	        		respondTweet = true;
	        		progress.setImage2(R.drawable.twittere);
	        		Toast.makeText(getApplicationContext(), "Replied to tweet owner", Toast.LENGTH_SHORT).show();
	        	}
	        	else if(msg.getData().getBoolean("status") && msg.getData().getInt("type")==Const.TYPE_PLURK) {
	        		respondPlurk=true;
	        		progress.setImage3(R.drawable.plurke);
	        		Toast.makeText(getApplicationContext(), "Response added", Toast.LENGTH_SHORT).show();
	        	}
	        	else {
	        		Toast.makeText(getApplicationContext(), "Could not add response", Toast.LENGTH_SHORT).show();
	        	}
	        	progress.hide();
	        	if(respondFacebookStatus || respondTweet || respondPlurk) {
		        	response.setText("");
	        		tID++;
					gotFacebookStatusResponse=false;
					gotTweetResponse=false;
					gotPlurkResponse=false;
					progress.setText("Retrieving responses...");
			        progress.setImage1(R.drawable.facebookd);
					progress.setImage2(R.drawable.twitterd);
					progress.setImage3(R.drawable.plurkd);
					progress.setImage1Visibility(View.GONE);
					progress.setImage2Visibility(View.GONE);
					progress.setImage3Visibility(View.GONE);
					if(currentType==Const.TYPE_FACEBOOK) {
						progress.setImage1Visibility(View.VISIBLE);
						showDialog(DIALOG_PROGRESS);
						new GetResponseThread(getApplicationContext(),recentResponseH,messageID,currentType,true,tID%Integer.MAX_VALUE).start();
					}
					else if(currentType==Const.TYPE_TWITTER) {
						progress.setImage2Visibility(View.VISIBLE);
						setResult(RESULT_OK);
			        	finish();
					}
					else if(currentType==Const.TYPE_PLURK) {
						progress.setImage3Visibility(View.VISIBLE);
						showDialog(DIALOG_PROGRESS);
						new GetResponseThread(getApplicationContext(),recentResponseH,messageID,currentType,false,tID%Integer.MAX_VALUE).start();
					}
	        	}
        	}
        }
    };
}

class DeleteStatusThread extends Thread {
	Handler mHandler;
	boolean status;
	int tID;
	String messageID;
	int type;
	
	public DeleteStatusThread(Handler mHandler,String messageID,int type, int tID) {
		this.mHandler=mHandler;
		this.messageID=messageID;
		this.tID=tID;
		this.type=type;
	}
	
	public void run() {
		status=false;
		if(type==Const.TYPE_FACEBOOK) {
			try {
				Log.d("Test","facebook delete post");
				if(Const.facebook.removePost(messageID)){
					Log.d("Test","facebook post deleted");
					status=true;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		else if(type==Const.TYPE_TWITTER) {
			try {
				Log.d("Test","twitter delete tweet");
				Const.twitter.destroyStatus(Long.parseLong(messageID));
				Log.d("Test","twitter deleted");
				status=true;
			} catch (TwitterException e) {
				e.printStackTrace();
			}
		}
		else if(type==Const.TYPE_PLURK) {
			try {
				Log.d("Test","plurk delete");
				JSONObject ret=Const.plurk.getPc().plurkDelete(messageID);
				try {
					ret.getString("success_text");
					Log.d("Test","plurk deleted");
					status=true;
				} catch (JSONException e) {
					e.printStackTrace();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		Message m=mHandler.obtainMessage();
		Bundle b=new Bundle();
		b.putInt("type",type);
		b.putBoolean("status", status);	
		b.putInt("tID", tID);	
		m.setData(b);
		mHandler.sendMessage(m);
	}
}

class RespondThread extends Thread {
	Handler mHandler;
	boolean status;
	int tID;
	String messageID;
	String message;
	int type;
	
	public RespondThread(Handler mHandler,String messageID,String message,int type, int tID) {
		this.mHandler=mHandler;
		this.messageID=messageID;
		this.tID=tID;
		this.type=type;
		this.message=message;
	}
	
	public void run() {
		status=false;
		if(type==Const.TYPE_FACEBOOK) {
			try {
				Log.d("Test","facebook response add");
				if(Const.facebook.addComment(messageID, message)) {
					status=true;
					Log.d("Test","facebook response added");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		else if(type==Const.TYPE_TWITTER) {
			try {
				Log.d("Test","twitter reply get");
				Const.twitter.updateStatus("@"+messageID+" "+message);
				status=true;
				Log.d("Test","twitter reply got");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		else if(type==Const.TYPE_PLURK) {
			try {
				Log.d("Test","plurk response add");
				JSONObject ret=Const.plurk.getPc().responseAdd(messageID, message, Qualifier.SAYS);
				try {
					ret.getString("error_text");
				} catch (JSONException e) {
					Plurk p=new Plurk(ret,false);
					p.setOwner(Const.plurk.getUser());
					Log.d("Test","plurk response added");
					if(Const.myPlurkResponses!=null) {
						Const.myPlurkResponses.add(p);
					}
					else {
						LinkedList<Plurk> list=new LinkedList<Plurk>();
						list.add(p);
						Const.myPlurkResponses=list;
					}
					status=true;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		Message m=mHandler.obtainMessage();
		Bundle b=new Bundle();
		b.putInt("type",type);
		b.putBoolean("status", status);	
		b.putInt("tID", tID);	
		m.setData(b);
		mHandler.sendMessage(m);
	}
	
}

class GetResponseThread extends Thread {
	Handler mHandler;
	boolean status;
	int tID;
	String messageID;
	int type;
	Context context;
	boolean accessInternet;
	
	public GetResponseThread(Context context,Handler mHandler,String messageID,int type,boolean accessInternet, int tID) {
		this.mHandler=mHandler;
		this.messageID=messageID;
		this.tID=tID;
		this.type=type;
		this.context=context;
		this.accessInternet=accessInternet;
	}
	
	public void run() {
		status=false;
		if(accessInternet) {
			if(type==Const.TYPE_FACEBOOK) {
				try {
					Log.d("Test", "facebook response");
					JSONArray wall=null;
					JSONArray profiles=null;
					JSONArray comments=null;
					wall=Const.facebook.getComments(messageID);
					comments=wall.getJSONObject(0).optJSONArray("fql_result_set");
					Const.myFacebookComments=FacebookStatus.getFacebookStatuses(comments);
					if(Const.myFacebookComments!=null) {
						profiles=wall.getJSONObject(1).optJSONArray("fql_result_set");
						List<FacebookUser> uList=FacebookUser.getFacebookUsers(profiles);
						if(Const.myFacebookUsers==null) {
							Const.myFacebookUsers = uList;
							for (FacebookUser u : Const.myFacebookUsers) {
								u.downloadPic();
							}
						}
						else {
							for (FacebookUser u : uList) {
								if (FacebookUser.getUser(Const.myFacebookUsers,
										u.getId()) == null) {
									u.downloadPic();
									Const.myFacebookUsers.add(u);
								}
							}
						}
						for(FacebookStatus s:Const.myFacebookComments) {
							s.setOwner(FacebookUser.getUser(Const.myFacebookUsers,s.getActorID()));
						}
					}
					Log.d("Test","facebook response get");
					status=true;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			else if(type==Const.TYPE_TWITTER) {
				try {
					Log.d("Test","twitter retweet get");
					Const.twitter.retweetStatus(Long.parseLong(messageID));
					Log.d("Test","twitter retweet got");
					status=true;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			else if(type==Const.TYPE_PLURK) {
				try {
					Log.d("Test", "plurk response");
					JSONObject resObj=Const.plurk.getPc().responseGet(messageID);
					JSONArray resArray = resObj.getJSONArray("responses");
					Const.myPlurkResponses=Plurk.getPlurks(resArray,false);
					if(Const.myPlurkResponses!=null) {
						JSONObject resUsersArray=resObj.getJSONObject("friends");
						List<PlurkUser> uList = PlurkUser.getUsers(context,
								resUsersArray);
						if (Const.myPlurkUsers == null) {
							Const.myPlurkUsers = uList;
							for (PlurkUser u : Const.myPlurkUsers) {
								u.downloadPic();
							}
						} else {
							for (PlurkUser u : uList) {
								if (PlurkUser.getUser(Const.myPlurkUsers, u
										.getUserID()) == null) {
									u.downloadPic();
									Const.myPlurkUsers.add(u);
								}
							}
						}
						for(Plurk p:Const.myPlurkResponses) {
							if(p.getUserID()!=Const.plurk.getUserID()) {
								p.setOwner(PlurkUser.getUser(Const.myPlurkUsers, p.getUserID()));
							}
							else {
								p.setOwner(Const.plurk.getUser());
							}
						}
					}
					Log.d("Test", "plurk response get");
					status=true;
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		}
		else {
			status=true;
		}
		Message m=mHandler.obtainMessage();
		Bundle b=new Bundle();
		b.putInt("type",type);
		b.putBoolean("status", status);	
		b.putInt("tID", tID);	
		m.setData(b);
		mHandler.sendMessage(m);
	}
	
}
